/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.service.datasource;

import org.apache.ibatis.io.VFS;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import yhao.infra.common.model.Entity;
import yhao.infra.service.datasource.mybatis.dialect.Dialect;
import yhao.infra.service.datasource.mybatis.interceptor.BaseInterceptor;
import yhao.infra.service.datasource.mybatis.interceptor.PaginationInterceptor;
import yhao.infra.service.datasource.mybatis.interceptor.SqlStatInterceptor;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Dao 通用配置工厂
 * <ul>
 *     <li>sqlSession配置定义</li>
 * </ul>
 *
 * @author yoara
 */
public class BaseDaoConfigFactory {
    //包名扫描，所有在该包下且继承了Entity的实体类可以自动别名化
    public static final String BASIC_MYBATIS_ALIASES_PACKAGE = "basic.mybatis.aliasesPackage";

    public static SqlSessionFactory makeSqlSessionFactory(
            Environment env, DataSource dataSource, Class<? extends Dialect> type) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        VFS.addImplClass(SpringBootVFS.class);

        //不生效，只能通过方法最后的set实现
        Properties properties = new Properties();
        properties.setProperty("cacheEnabled","false");
        properties.setProperty("defaultStatementTimeout","25");
        bean.setConfigurationProperties(properties);

        BaseInterceptor multiDataSourceInterceptor = new BaseInterceptor();

        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        Properties pageProperties = new Properties();
        pageProperties.setProperty(PaginationInterceptor.DIALECT_CLASS_PROPERTY, type.getName());
        paginationInterceptor.setProperties(pageProperties);

        Interceptor[] plugins = new Interceptor[3];
        plugins[0] = multiDataSourceInterceptor;
        plugins[1] = paginationInterceptor;
        plugins[2] = new SqlStatInterceptor();
        bean.setPlugins(plugins);

        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage(env.getProperty(BASIC_MYBATIS_ALIASES_PACKAGE));
        bean.setTypeAliasesSuperType(Entity.class);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("classpath*:mybatis/**/*.xml"));

        SqlSessionFactory factory = bean.getObject();
        factory.getConfiguration().setCacheEnabled(false);
        factory.getConfiguration().setDefaultStatementTimeout(25);

        return factory;
    }
}
